# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libmariadbclient
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.7
pkgrel=1
pkgdesc="MariaDB client libraries (mingw-w64)"
arch=('any')
url="https://mariadb.org/"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
#optdepends=("${MINGW_PACKAGE_PREFIX}-curl")
options=('!strip' 'staticlibs')
source=(#"https://downloads.mariadb.org/interstitial/connector-c-${pkgver}/source-tgz/mariadb-connector-c-${pkgver}-src.tar.gz"
        http://mirror.mephi.ru/mariadb/connector-c-${pkgver}/mariadb-connector-c-${pkgver}-src.tar.gz{,.asc}
        '001-2.2.3-fix-libnames-mingw.patch'
        '002-check-also-for-__MINGW64__-definition-to-decide-targ.patch'
        'use_fopen_for_xp_compatibility.patch'
        'fix-size-t-defined.patch'
        'fix-redefinitions.patch'
        'fix-pthread.patch'
        'add-mariadb_config.patch')
validpgpkeys=("199369E5404BD5FC7D2FE43BCBCB082A1BB943DB") #MariaDB Package Signing Key <package-signing-key@mariadb.org>
sha256sums=('94f9582da738809ae1d9f1813185165ec7c8caf9195bdd04e511f6bdcb883f8e'
            'SKIP'
            '9b7a6e0d38e9fcf0e514871fe34d47402973b81fafccc90ecdaed3ee8441b34c'
            '72dcc3e33189056ebb383fab0fea3b85c26919cbb58b5f1dead1b8b614e43c48'
            '8069bc0c7f4204fe2b1ea54c610c27aebd0c54d7a21ecf6352c9bdd9cf1c4062'
            'e84d3ac9b2e716dc1581d82c34001d8561bb0d09750d1d57d4b448a8cc259ff0'
            'ddb20e474ba1a63f65f28ee76bb74074316b15cc90ecdae6d2aa1febf0803d72'
            'd69bded191fad678728ac1f9c79e48daf2ed10c83ee9698506dbb0abf8489575'
            'c9fca6bcb6747b5431a6afdb6c12c9f8af205c1d289d323d67108b1cbeaf5a7b')

prepare() {
  cd ${srcdir}/mariadb-connector-c-${pkgver}-src
  patch -p1 -i ${srcdir}/001-2.2.3-fix-libnames-mingw.patch
  patch -p1 -i ${srcdir}/002-check-also-for-__MINGW64__-definition-to-decide-targ.patch
  patch -p1 -i ${srcdir}/use_fopen_for_xp_compatibility.patch
  patch -p1 -i ${srcdir}/fix-size-t-defined.patch
  patch -p1 -i ${srcdir}/fix-redefinitions.patch
  patch -p1 -i ${srcdir}/fix-pthread.patch
  patch -p1 -i ${srcdir}/add-mariadb_config.patch
}

build() {
  rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPREFIX_INSTALL_DIR=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DWITH_EXTERNAL_ZLIB=ON \
    -DWITH_REMOTEIO=OFF \
    -DWITH_OPENSSL=ON \
    -DWITH_MYSQLCOMPAT=OFF \
    ../mariadb-connector-c-${pkgver}-src
  #Fix config files for local system
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" -i mariadb_config/mariadb_config.c
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  install -D -m0755 mariadb_config/mariadb_config.exe ${pkgdir}${MINGW_PREFIX}/bin/mariadb_config.exe

  ln -s "${pkgdir}${MINGW_PREFIX}"/include/mariadb "${pkgdir}${MINGW_PREFIX}"/include/mysql

  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient_r}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient}.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient_r}.a
# Fix path references in include files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" -i "${pkgdir}${MINGW_PREFIX}"/include/mariadb/my_config.h
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" -i "${pkgdir}${MINGW_PREFIX}"/include/mysql/my_config.h
 }
